package com.lu.system.user.controller;

import java.util.List;
import java.util.Map;

import com.lu.common.utils.PageUtils;
import com.lu.common.utils.Query;
import com.lu.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lu.system.user.domain.UserRoleDO;
import com.lu.system.user.service.UserRoleService;

/**
 * 用户与角色对应关系
 * 
 * @author zhanglu
 * @email 1309617271@qq.com
 * @date 2018-09-20 21:05:03
 */
 
@Controller
@RequestMapping("/system/userRole")
public class UserRoleController {
	@Autowired
	private UserRoleService userRoleService;

   /**
    *  根据id查询
	*  @return
	*/
    @ResponseBody
    @GetMapping("/get")
    public R get(Long id){
        UserRoleDO userRole = userRoleService.get(id);
        R r = new R();
        r.put("data",userRole);
        return r;
    }

	/**
	 * 查询列表数据
	 * @param params
	 * @return
	 */
	@ResponseBody
	@GetMapping("/list")
	public PageUtils getUserRoleList(@RequestParam Map<String, Object> params){
        Query query = new Query(params);
		List<UserRoleDO> userRoleList = userRoleService.list(query);
		int total = userRoleService.count(query);
		PageUtils pageUtils = new PageUtils(userRoleList, total);
		return pageUtils;
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( UserRoleDO userRole){
		if(userRoleService.save(userRole)>0){
			return R.ok();
		}
		return R.error();
	}

    /**
     * 批量保存
     */
    @ResponseBody
    @PostMapping("/saveBatch")
    public R saveBatch( List<UserRoleDO> userRole){
        try {
			userRoleService.saveBatch(userRole);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
        return R.ok();
    }

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( UserRoleDO userRole){
		userRoleService.update(userRole);
		return R.ok();
	}

    /**
     * 批量修改
     */
    @ResponseBody
    @PostMapping("/updateBatch")
    public R updateBatch( List<UserRoleDO> userRole){
        try {
			userRoleService.updateBatch(userRole);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
        return R.ok();
    }

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	public R remove( Long id){
		if(userRoleService.remove(id)>0){
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 批量删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] ids){
		userRoleService.batchRemove(ids);
		return R.ok();
	}
	
}
